#!/bin/bash
# ============================================================================
# 清理 ccache 缓存脚本
# ============================================================================
# 用法:
#   ./clean_ccache.sh                    # 清理所有 ccache 缓存（默认行为）
#   ./clean_ccache.sh all                # 清理所有 ccache 缓存（显式指定）
#   ./clean_ccache.sh debug              # 只清理 Debug 构建的缓存
#   ./clean_ccache.sh release            # 只清理 Release 构建的缓存
#   ./clean_ccache.sh dynamic-debug      # 只清理 Dynamic Debug 构建的缓存
#   ./clean_ccache.sh dynamic-release    # 只清理 Dynamic Release 构建的缓存
# ============================================================================

# 获取脚本所在目录（项目根目录）
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

# 解析参数
CLEAN_MODE="${1:-all}"  # 默认清理所有

echo "========================================"
echo "清理 ccache 缓存"
echo "========================================"

cleaned_count=0

# 清理指定构建目录的 ccache
clean_build_type() {
    local build_type=$1
    local build_dir="Build/build64_${build_type}"
    
    if [ -d "$build_dir" ]; then
        local ccache_dir="$build_dir/.ccache"
        if [ -d "$ccache_dir" ]; then
            echo "清理: $ccache_dir"
            rm -rf "$ccache_dir"
            cleaned_count=$((cleaned_count + 1))
        else
            echo "跳过: $ccache_dir (目录不存在)"
        fi
    else
        echo "跳过: $build_dir (构建目录不存在)"
    fi
}

# 根据参数决定清理范围
if [ "$CLEAN_MODE" = "all" ] || [ -z "$1" ]; then
    # 清理所有构建目录下的 .ccache
    echo "模式: 清理所有 ccache 缓存"
    echo ""
    
    # 清理所有构建类型
    clean_build_type "debug"
    clean_build_type "release"
    clean_build_type "dynamic_debug"
    clean_build_type "dynamic_release"
    
    # 也清理项目根目录下的旧 .ccache（向后兼容）
    if [ -d ".ccache" ]; then
        echo "清理: .ccache (旧位置)"
        rm -rf ".ccache"
        cleaned_count=$((cleaned_count + 1))
    fi
    
elif [ "$CLEAN_MODE" = "debug" ]; then
    # 只清理 Debug 构建的缓存
    echo "模式: 只清理 Debug 构建的缓存"
    echo ""
    clean_build_type "debug"
    
elif [ "$CLEAN_MODE" = "release" ]; then
    # 只清理 Release 构建的缓存
    echo "模式: 只清理 Release 构建的缓存"
    echo ""
    clean_build_type "release"
    
elif [ "$CLEAN_MODE" = "dynamic-debug" ]; then
    # 只清理 Dynamic Debug 构建的缓存
    echo "模式: 只清理 Dynamic Debug 构建的缓存"
    echo ""
    clean_build_type "dynamic_debug"
    
elif [ "$CLEAN_MODE" = "dynamic-release" ]; then
    # 只清理 Dynamic Release 构建的缓存
    echo "模式: 只清理 Dynamic Release 构建的缓存"
    echo ""
    clean_build_type "dynamic_release"
    
else
    echo "错误: 未知的参数 '$CLEAN_MODE'"
    echo ""
    echo "用法:"
    echo "  ./clean_ccache.sh                    # 清理所有 ccache 缓存（默认）"
    echo "  ./clean_ccache.sh all                # 清理所有 ccache 缓存"
    echo "  ./clean_ccache.sh debug              # 只清理 Debug 构建的缓存"
    echo "  ./clean_ccache.sh release            # 只清理 Release 构建的缓存"
    echo "  ./clean_ccache.sh dynamic-debug      # 只清理 Dynamic Debug 构建的缓存"
    echo "  ./clean_ccache.sh dynamic-release    # 只清理 Dynamic Release 构建的缓存"
    echo "========================================"
    exit 1
fi

if [ $cleaned_count -gt 0 ]; then
    echo ""
    echo "✅ 已清理 $cleaned_count 个 ccache 缓存目录"
else
    echo ""
    echo "ℹ️  没有找到需要清理的 ccache 缓存目录"
fi

echo "========================================"
